<?php session_start() ?>
<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
   <title>添加好友</title>
   <link href="../css/bootstrap.min.css" rel="stylesheet">
   <script type="text/javascript">
   	function isAddFriend(isAddFriend,requester,receiver){
		var tips = document.getElementsByName("tip");
		for(var i = 0; i<tips.length; i++){//清空提示
			tips[i].innerHTML = "";	
		}
		if(isAddFriend == false){
			document.getElementById(receiver).innerHTML = "你俩已是好友!";
			return false;
			
		}else{
			var xmlhttp;
			if (window.XMLHttpRequest)
  			{// code for IE7+, Firefox, Chrome, Opera, Safari
  				xmlhttp=new XMLHttpRequest();
 			 }
			else
  			{// code for IE6, IE5
 				xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  			}
			xmlhttp.onreadystatechange=function()
  			{
 				if (xmlhttp.readyState==4 && xmlhttp.status==200)
    			{
    				document.getElementById(receiver).innerHTML = "请求已发送!";
    			}
  			}
			xmlhttp.open("GET","../handle/do_add_friends.php?requester="+
			requester+"&receiver="+receiver);
			xmlhttp.send();
			return true;
		}
	}
   </script>
</head>
<body>
<?php require '../public/header.php'?>
<!-- 导航栏 -->
<ul class="nav nav-pills">
   <li><a href="home.php">主页</a></li>
   <li><a href="message_record.php">留言记录</a></li>
   <li><a href="friends.php">我的好友</a></li>
   <li class="active"><a href="#">添加好友</a></li>
   <li><a href="notice.php">我的消息</a></li>
</ul><hr/>
<div class="panel panel-info"> 	
	<div class="panel-heading" id="message_record">
     	<h3 class="panel-title">所有用户</h3>
   </div>
	<div class="panel-body">
		<?php 
		require '../public/conn.php';
		function check_is_friends($requester,$receiver){
			$query1="SELECT * FROM friends WHERE (friendA='$receiver' AND 
			friendB='$requester') OR (friendA='$requester' AND friendB='$receiver')";
			$result1 = mysql_query($query1);
			if($result1 != false){
				if($row1 = mysql_fetch_array($result1)){//已是好友,返回false
					return false;
				}
			}
			return true;
		}
		if(isset($_SESSION['username'])){//已登陆
			$username=$_SESSION['username'];
			
			$result = mysql_query("SELECT * FROM users");
			while($row = mysql_fetch_array($result))//读取好友记录并显示
  			{ 
				if($username == $row['username']){
					continue;	
				}
				echo "<div>";
				echo "<div style='width:40%;float:left;'>
						<a href='#'>".$row['username']."</a>
					  </div>";
					  $B = $row['username'];
					  $requester = $username; //好友申请者
					  $receiver = $row['username'];//好友接受者
					  $isAddFriend = 0;
					  if(check_is_friends($username,$B)){
					  	$isAddFriend = 1;
					  }
				echo '<div style="width:40%;float:left;">
						<button type="button" class="btn btn-primary" 
								onClick="return isAddFriend('.$isAddFriend.",'"                                  .$requester."','".$receiver."'".')">
				        		加为好友													                        </button>
						<label id="'.$B.'" name="tip" style="color:red;margin-left:20px"></label>
					  </div>';
				echo '</div><br/><br/>';
 	 		}
			mysql_close($con);
		}else{		//未登陆
			echo "亲,先去<a href='../face/login-face.php'><span>登陆</span>                </a>吧！";
		}
	    ?>
   </div>
</div>
<?php require('../public/footer.php');?>
</body>
</html>